#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
using namespace std;
template <class T>
class queue {
private:
	T data;
	queue* next;
public:
	queue(T _data = 0) {
		data = _data;
		next = nullptr;
	}
	~queue() {}
	void push_back(T _data = 0) {
		queue* tmp = this;
		while (tmp->next)tmp = tmp->next;
		tmp->next = new queue(_data);
	}
	T pop() {
		int i = data;
		data = next->data;
		next = next->next;
		return i;
	}
	void print() {
		queue* tmp = this;
		while (tmp) {
			cout << tmp->data << " ";
			tmp = tmp->next;
		}
		cout << endl;
	}
	queue* reverse(int k) {
		queue* cur = this;
		T* arr=new T[100];
		int i = 0;
		while (i<k) {
			arr[i++] = cur->pop();
		}
		queue* tmp = new queue(arr[--i]);
		i--;
		while (i >= 0) {
			tmp->push_back(arr[i--]);
		}
		queue* q = tmp;
		while (q->next) {
			q = q->next;
		}
		q->next = cur;
		return tmp;
	}
};
int main(){
	queue<int>* q=new queue<int>(1);
	q->push_back(2);
	q->push_back(3);
	q->push_back(4);
	q->push_back(5);
	q->push_back(6);
	q->push_back(7);
	q->push_back(8);
	q->push_back(9);
	q->print();
	q=q->reverse(7);
	q->print();
	return 0;
}